package com.hu.hutool.controller;

import cn.hutool.core.lang.UUID;
import com.commons.tools.utils.Result;
import com.hu.hutool.dto.UserDto;
import com.hu.hutool.dto.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author CazCen
 * @Date 2022/1/14 0:29
 */
@RestController
@RequestMapping("/auth/info")
@Api(tags = "mqtt  http 认证")
@Slf4j
public class MqttHttpAuth {

    @PostMapping("get/user/info")
    @ApiOperation("获取用户信息")
    public Integer mqttAuth(HttpServletRequest request, HttpServletResponse response) {


        log.info("我是 mtqq 的认证连接请求 .....");


        String clientId = request.getParameter("clientid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        log.info("clientid:{} ；username =  {}，password = {} ",clientId,username,password);

        UUID uuid = UUID.randomUUID();
        log.info("uuid = {}",uuid.toString().replace("-",""));

        if(clientId == null || "".equals(clientId.trim()) || username == null || "".equals(username) || password == null || "".equals(password.trim())) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return 0;
        }
        if(StringUtils.startsWithIgnoreCase("root","") && "123456".equals(password)) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return 0;
        }

        return 1;
    }


    @PostMapping("offline/webhook")
    @ApiOperation("获取用户信息")
    public Integer webhook(HttpServletRequest request,
                           HttpServletResponse response,
                           @RequestBody Map<String,Object> params) {

        log.info("我是 mtqq 的 webhook 连接请求 ..... params :{}",params);


        log.info("request ..... request :{}",request);

        log.info("response ..... response :{}",response);

        // mqtt client offline hook ...
        String clientId = request.getParameter("clientid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        String action = (String) params.get("action");
        username = (String) params.get("username");

        UUID uuid = UUID.randomUUID();
        String uuid2 = uuid.toString().replace("-", "");
        log.info("uuid = {}",uuid2);

        clientId = (String) params.get("clientid");
        if(action.equals("client_connected")){
            log.info("客户端{}接入本系统"+clientId);
        }

        if(action.equals("client_disconnected")){
            log.info("客户端{}下线"+clientId);
        }

        log.info("clientid:{} ；username =  {}，password = {} ",clientId,username,password);

        return 1;
    }

}
